<?php

class Default_Plugin_BasicInit extends Zend_Controller_Plugin_Abstract{
	protected $_user = null;
	protected $_request = null;
	public function preDispatch(Zend_Controller_Request_Abstract $request){
		$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
    	$options = $bootstrap->getOptions();

		// set DEfault time zone
		date_default_timezone_set($options['timezone']);

		$this->_request = $request;
		$this->_initBaseUrl();
        $this->_initUser();


		// set acces to the admin part
		$module = $request->getModuleName();
		if('admin' == $module){
      		// get role
			if( 'admin' != $this->_user->getRole() ){
				// redirect to login
				$this->_goToLogin();
			}
		}
	}
	protected function _initBaseUrl(){
		if(!defined('BASE_URL')){
			define('BASE_URL',Zend_Controller_Front::getInstance()->getBaseUrl());
		}
	}
    protected function _initUser(){
        $this->_user = Zend_Registry::get("User");   
    }
    

	protected function  _goToLogin(){
	   $this->_request->setModuleName('default');
		$this->_request->setControllerName('user');
		$this->_request->setActionName('login');
	}  
   
    
    

}